﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using Cashier_Interface;
using System.Data;

namespace Cashier.usercontrols
{
    public class DataGridViewExt: DataGridView
    {
        private IDataGridViewData iGridViewData;

        /// <summary>
        /// 构造函数
        /// </summary>
        public DataGridViewExt()
            : base()
        {
            this.DoubleBuffered = true;
        }

        private Object implementsInstance;

        [Category("Custom Attribute")]
        [Description("IDataGridViewData接口实现类")]
        [Browsable(false)]
        public Object ImplementsInstance
        {
            get { return implementsInstance; }
            set { implementsInstance = value; }
        }

        private int pageindex = 1;
        
        [Category("Custom Attribute")]
        [Description("起始页码，默认值1")]
        [Browsable(false)]
        public int Pageindex
        {
            get { return pageindex; }
            set { pageindex = value; }
        }

        private long recordcount;

        [Category("Custom Attribute")]
        [Description("总记录数，默认值0")]
        [Browsable(false)]
        public long Recordcount
        {
            get { return recordcount; }
            set { recordcount = value; }
        }

        private int pagesize = 100;

        [Category("Custom Attribute")]
        [Description("每页记录数，默认值100")]
        [Browsable(false)]
        public int Pagesize
        {
            get { return pagesize; }
            set { pagesize = value; }
        }

        private int pagecount;

        /// <summary>
        /// 加载数据
        /// </summary>
        public void LoadingData()
        {
            iGridViewData = (IDataGridViewData)implementsInstance;
            iGridViewData.GenerateGridViewColumns(this);
            this.recordcount = iGridViewData.GetBindingDataRecord();
            if (recordcount > 0 && pagesize > 0)
            {
                if (recordcount % pagesize > 0)
                    pagecount = (int)(recordcount / pagesize + 1);
                else
                    pagecount = (int)(recordcount / pagesize);
            }
            int rowindex = 0;
            if (this.SelectedRows.Count > 0)
            {
                if (this.SelectedRows[0].Index >= (recordcount - (pageindex - 1) * pagesize))
                    rowindex = (int)(recordcount - (pageindex - 1) * pagesize - 1);
                else
                    rowindex = this.SelectedRows[0].Index;
            }
            iGridViewData.GetBindingData(rowindex, pageindex, pagesize);
        }

        /// <summary>
        /// 左右键翻页事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            int keyValue = e.KeyValue;
            if (keyValue == 37)
            {
                if (pageindex > 1)
                {
                    pageindex -= 1;
                    iGridViewData.GetBindingData(0, pageindex, pagesize);
                }
                
            }
            else if (keyValue == 39)
            {
                if (pagecount > 1 && pageindex < pagecount)
                {
                    pageindex += 1;
                    iGridViewData.GetBindingData(0, pageindex, pagesize);
                }
            }
        }

        /// <summary>
        /// 处理Enter键响应
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            bool b = base.ProcessCmdKey(ref msg, keyData);
            if (keyData == Keys.Enter)
            {
                b = true;
                iGridViewData.EnterkeyEventHandler();
            }
            return b;
        }

        /// <summary>
        /// 重写DataGridView格式化方法
        /// </summary>
        /// <param name="e"></param>
        protected override void OnCellFormatting(DataGridViewCellFormattingEventArgs e)
        {
            base.OnCellFormatting(e);
            if (e.Value != null)
            {
                String typeName = e.Value.GetType().Name;
                if (typeName.Equals("Decimal"))
                    e.Value = ((decimal)e.Value).ToString("f2");
                else if (typeName.Equals("Boolean"))
                    e.Value = ((bool)e.Value) ? "是" : "否";
                else if (typeName.Equals("DateTime"))
                    e.Value = ((DateTime)e.Value).ToString("yyyy-MM-dd");
            }
        }
    }
}
